/**
 * @author Valter
 */



package persistence.connection;

import java.sql.*;
import java.lang.reflect.*;
import java.util.*;

public abstract class ConnectionManager
{	
	   
	  public  static Connection con;
	  public  static Statement stmt;
      
      /** TBThisLoader attribute below make part of the variable part */
      protected static DBSpecificConnectionDetail dbSpecificConnectionDetail;
      
      protected String userName;
      protected String password;
      protected String databaseName;
    
      
	   public ConnectionManager(){
	   	
	   	dbSpecificConnectionDetail = setSpecificDB(); /** variable part */
	    userName = dbSpecificConnectionDetail.getUserName();
	   	password = dbSpecificConnectionDetail.getPassword();

	   }

	   

     /**
      * TBThisLoader method below (setSpecificDatabase) is an extension point,
      *  and it makes part of the variable part.
      */  
     public abstract String setSpecificDatabase(); 
    
     /**
      * TBThisLoader method is not an extension point. But, it makes part of
      * the variable part.
      */
     
     
     public String getDatabaseName(){
     	return this.databaseName;
     }
     
     public void setDatabaseName(String databaseName){
     	this.databaseName = databaseName;
     }
     
     public DBSpecificConnectionDetail setSpecificDB(){
    
     try
	 {
     	
     	String databaseName = setSpecificDatabase();
     	String databaseNameUpperCase = databaseName.toUpperCase();
     	
     	if (databaseNameUpperCase.equals("SYBASE")){
     		
     		return new SyBaseConnection();
     		
     	}
     	else if(databaseNameUpperCase.equals("INTERBASE")){
     		
     		return new InterbaseConnection();
     		
     	}
     	else if(databaseNameUpperCase.equals("MYSQL")){
     		
     		return new MySQLConnection();
     		
     	}
     	else {
     		
     		throw new InvalidDatabaseNameException();
     	}
	 }catch(InvalidDatabaseNameException e){
	 	System.out.println("CATCH : ConnectionManager : " + e.getMessage());
	 	return null;
	 }
     }
     

     public abstract void ConnectDB();
     
     public abstract void DisconnectDB();

   

    public static Statement getStatement()
    {   
     return stmt;
    }
    
    public static Connection getConnection()
    {
      return con;
    }
    
    public void setConnection(Connection con1){
    	con = con1;
    }
    
   
}
